import cv2

from ultralytics import YOLO

# Load a COCO-pretrained YOLO11n model
model = YOLO("/data/yolo11/gxhyolo/runs/detect/train8/weights/best.pt")

# Train the model on the COCO8 example dataset for 100 epochs
#results = model.train(data="/data/yolo11/gxhyolo/datasets/gxh.yaml", epochs=1000, imgsz=640)

# Load an image from file
image_path = "/data/yolo11/gxhyolo/datasets/gxh/images/test/test01.png"
image = cv2.imread(image_path)
cv2.imshow("hello", image)

# Run inference with the YOLO11n model on the 'bus.jpg' immaage
results = model(image_path)
#results[0].show()

for result in results:
    
    boxes = result.boxes  # Get the bounding boxes
    #boxes_count = boxes.size;
    print("===========================" )
    for box in boxes:
        # Get the coordinates of the bounding box
        x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
        
        # Calculate the center coordinates
        center_x = int((x1 + x2) / 2)
        center_y = int((y1 + y2) / 2)
        
        # Print the center coordinates
        print("        ????????????????????????????" )
        print(f"          Center coordinates of the detected object: ({center_x}, {center_y})")
        
        # Optionally, draw the center on the image
        cv2.circle(image, (center_x, center_y), 5, (0, 255, 0), -1)

# Display the image with the center points (optional)
cv2.imshow("Detected Objects", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
